/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogVehi.java
 *
 * Created on 30/01/2011, 11:33:20 AM
 */

package com.catosist.run.application.vehiculo;

import com.catosist.run.application.vehiculo.gestion.GestionVehiculo;
import com.catosist.run.business.gestorcolores.GestorColores;
import com.catosist.run.business.gestormarca.GestorMarcas;
import com.catosist.run.business.gestorpersona.GestorPersona;
import com.catosist.run.business.gestorruta.GestorRuta;
import com.catosist.run.business.gestorvehiculo.GestorVehiculo;
import com.catosist.run.business.gestorviaje.GestorViaje;
import com.catosist.run.services.ColoresDTO;
import com.catosist.run.services.MarcasDTO;
import com.catosist.run.services.PersonaDTO;
import com.catosist.run.services.VehiculoDTO;
import com.catosist.run.services.ViajeDTO;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.NonUniqueResultException;
import org.springframework.dao.DataIntegrityViolationException;
/**
 *
 * @author Carlos
 */
public class DialogVehi extends javax.swing.JDialog {

    /** Creates new form DialogVehi */
    public DialogVehi(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    DialogVehi (java.awt.Frame parent, boolean modal, int IdVehi) {
        super(parent, modal);
        initComponents();

        this.Id = IdVehi;
    }

    DialogVehi(java.awt.Frame parent, boolean modal, int IdVehi, GestorVehiculo gestorVehiculo,
            GestorColores gestorColores, GestorMarcas gestorMarcas) {
        super(parent, modal);
        initComponents();

        this.gestorColores = gestorColores;
        this.gestorMarcas = gestorMarcas;
        this.gestorVehiculo = gestorVehiculo;
        this.Id = IdVehi;
        this.gestionVehiculo = new GestionVehiculo();

        this.combustibles = new String[2];
        

        this.agregarActividad();
        this.agregarColor();
        this.agregarMarca();
        this.agregarOrigen();
        this.agregarCombustible();
        System.out.println("llegaaa!!");
        if(this.Id >0){

            VehiculoDTO vehiculoDTO = gestorVehiculo.getById(Id);
            this.setTitle("Edicion del vehiculo " + vehiculoDTO.getPlaca());

            System.out.println("entraaa");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println(Id);
//            int codigo = gestionVehiculo.getSelectedId();
//            System.out.println("el Id =  "+ codigo);
            //System.out.println(gestorVehiculo.getVOatIndex(gestionVehiculo.getSelectedId()).getCodigo());
//            this.txtCombustible.setText(vehiculoDTO.getCombustible());
//            System.out.println(gestorVehiculo.getVOatIndex(gestionVehiculo.getSelectedId()).getCombustible());
//            System.out.println(gestorVehiculo.getVOatIndex(gestionVehiculo.getSelectedId()).getImagen());
            this.txtModeloAnio.setText(vehiculoDTO.getModelo());
//            System.out.println(gestorVehiculo.getVOatIndex(gestionVehiculo.getSelectedId()).getModelo());
            this.txtPlaca.setText(vehiculoDTO.getPlaca());
//            System.out.println(gestorVehiculo.getVOatIndex(gestionVehiculo.getSelectedId()).getPlaca());
            this.jDateChooser1.setDate(vehiculoDTO.getFingreso());

            int indice = 0;
            //Seleccionar el tipo de combistible
            for (int i = 0; i < cmbCombustible.getItemCount(); i++) {
                if (cmbCombustible.getItemAt(i).toString().compareTo(vehiculoDTO.getCombustible()) == 0) {
                    indice = i;
                }
            }
            cmbCombustible.setSelectedIndex(indice);

            //Seleccionar la actividad
            String actividad;
            if (vehiculoDTO.getActivo()) {
                actividad = "Activo";
            }
            else
                actividad = "Inactivo";
            for (int i = 0; i < cmboxActivo.getItemCount(); i++) {
                if (cmboxActivo.getItemAt(i).toString().compareTo(actividad) == 0) {
                    indice = i;
                }
            }
            cmboxActivo.setSelectedIndex(indice);

            //Seleccionar el origen
            for (int i = 0; i < cmboxDepartamento.getItemCount(); i++) {
                if (cmboxDepartamento.getItemAt(i).toString().compareTo(vehiculoDTO.getOrigen()) == 0) {
                    indice = i;
                }
            }
            cmboxDepartamento.setSelectedIndex(indice);

            //Seleccionar la marca
            for (int i = 0; i < cmboxMarca.getItemCount(); i++) {
                if (cmboxMarca.getItemAt(i).toString().compareTo(vehiculoDTO.getMarca()) == 0) {
                    indice = i;
                }
            }
            cmboxMarca.setSelectedIndex(indice);
  
            //Seleccionar el color
            for (int i = 0; i < cmboxColor.getItemCount(); i++) {
                if (cmboxColor.getItemAt(i).toString().compareTo(vehiculoDTO.getColor()) == 0) {
                    indice = i;
                }
            }
            cmboxColor.setSelectedIndex(indice);
        }
        else
            this.setTitle("Nuevo vehiculo");
        this.active = false;
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        txtModeloAnio = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtPlaca = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cmboxDepartamento = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cmboxActivo = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        cmboxMarca = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        cmboxColor = new javax.swing.JComboBox();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        cmbCombustible = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel3.setText("Fecha de Ingreso:");
        jLabel3.setName("jLabel3"); // NOI18N

        jDateChooser1.setName("jDateChooser1"); // NOI18N

        jLabel4.setText("Modelo:");
        jLabel4.setName("jLabel4"); // NOI18N

        txtModeloAnio.setName("txtModeloAnio"); // NOI18N

        jLabel6.setText("Placa:");
        jLabel6.setName("jLabel6"); // NOI18N

        txtPlaca.setName("txtPlaca"); // NOI18N

        jLabel7.setText("Origen:");
        jLabel7.setName("jLabel7"); // NOI18N

        cmboxDepartamento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmboxDepartamento.setName("cmboxDepartamento"); // NOI18N

        jLabel8.setText("Combustible:");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText("Actividad:");
        jLabel9.setName("jLabel9"); // NOI18N

        cmboxActivo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmboxActivo.setName("cmboxActivo"); // NOI18N

        jLabel10.setText("Marcas:");
        jLabel10.setName("jLabel10"); // NOI18N

        cmboxMarca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmboxMarca.setName("cmboxMarca"); // NOI18N

        jLabel11.setText("Colores:");
        jLabel11.setName("jLabel11"); // NOI18N

        cmboxColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmboxColor.setName("cmboxColor"); // NOI18N

        btnAceptar.setText("Aceptar");
        btnAceptar.setName("btnAceptar"); // NOI18N
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        cmbCombustible.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbCombustible.setName("cmbCombustible"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(49, 49, 49))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtModeloAnio)
                            .addComponent(txtPlaca)
                            .addComponent(cmboxColor, 0, 101, Short.MAX_VALUE)
                            .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel10)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmboxDepartamento, javax.swing.GroupLayout.Alignment.LEADING, 0, 115, Short.MAX_VALUE)
                            .addComponent(cmboxActivo, javax.swing.GroupLayout.Alignment.LEADING, 0, 115, Short.MAX_VALUE)
                            .addComponent(cmbCombustible, javax.swing.GroupLayout.Alignment.LEADING, 0, 115, Short.MAX_VALUE)
                            .addComponent(cmboxMarca, javax.swing.GroupLayout.Alignment.LEADING, 0, 115, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(cmbCombustible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtModeloAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmboxActivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(cmboxDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(cmboxMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmboxColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:

        if(this.cmboxActivo.getSelectedIndex() == 0){
            this.active = true;
        }

        VehiculoDTO vehiculoDTO = new VehiculoDTO();
        vehiculoDTO.setActivo(active);
        vehiculoDTO.setColorId(gestorColores.getIdAtIndex(cmboxColor.getSelectedIndex()));
//        vehiculoDTO.setCombustible(txtCombustible.getText());
        vehiculoDTO.setCombustible(cmbCombustible.getSelectedItem().toString());
        vehiculoDTO.setFingreso(jDateChooser1.getDate());
        vehiculoDTO.setId(Id);
        vehiculoDTO.setMarcaId(gestorMarcas.getIdAtIndex(cmboxMarca.getSelectedIndex()));
        vehiculoDTO.setModelo(txtModeloAnio.getText().toString());
        vehiculoDTO.setOrigen(cmboxDepartamento.getSelectedItem().toString());
        vehiculoDTO.setPlaca(txtPlaca.getText());

        try {
            gestorVehiculo.saveOrUpdate(vehiculoDTO);
            this.setVisible(false);
        } catch (DataIntegrityViolationException e) {
            JOptionPane.showMessageDialog(this, "Ya existe un vehiculo con esa placa", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
        
        


    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void agregarOrigen(){
        DefaultComboBoxModel model = new DefaultComboBoxModel((new Object[]{"p", "o"}));
        cmboxDepartamento.setModel(model);
    }

    private void agregarCombustible(){
        DefaultComboBoxModel model = new DefaultComboBoxModel((new Object[]{"g", "p"}));
        cmbCombustible.setModel(model);
    }

    private void agregarMarca(){
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Iterator<MarcasDTO> it = gestorMarcas.getAll().iterator(); it.hasNext();) {
            MarcasDTO marcasDTO = it.next();
            model.addElement(marcasDTO.getNombre());

        }
        this.cmboxMarca.setModel(model);
    }

    private void agregarColor(){
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Iterator<ColoresDTO> it = gestorColores.getAll().iterator(); it.hasNext();) {
            ColoresDTO coloresDTO = it.next();
            model.addElement(coloresDTO.getNombre());

        }
        this.cmboxColor.setModel(model);
    }

    private void agregarActividad(){
        DefaultComboBoxModel model = new DefaultComboBoxModel((new Object[]
        {"Activo","Inactivo"}));
        cmboxActivo.setModel(model);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogVehi dialog = new DialogVehi(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbCombustible;
    private javax.swing.JComboBox cmboxActivo;
    private javax.swing.JComboBox cmboxColor;
    private javax.swing.JComboBox cmboxDepartamento;
    private javax.swing.JComboBox cmboxMarca;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtModeloAnio;
    private javax.swing.JTextField txtPlaca;
    // End of variables declaration//GEN-END:variables
    private int Id;

    private GestorMarcas gestorMarcas;
    private GestorVehiculo gestorVehiculo;
    private GestorColores gestorColores;
    private GestionVehiculo gestionVehiculo;
    private Boolean active;
    private String[] combustibles;
    private String[] origenes;
    private String[] actividades;
}
